package com.woniuxy.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.pojo.Course;
import com.woniuxy.pojo.Plan;
import com.woniuxy.pojo.Staff;
import com.woniuxy.service.CourseService;
import com.woniuxy.service.PlanService;
import com.woniuxy.service.StaffService;

@Controller
@RequestMapping("/train")
public class PlanController {

	@Autowired
	private PlanService planService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private StaffService staffService;
	
	@RequestMapping("/list")
	public String getAllPlan(@RequestParam(required = false, defaultValue = "1") Integer startPage,
            @RequestParam(required = false, defaultValue = "5") Integer PageSize,Model model,HttpServletRequest req) {
		
		String class_name = null;
		if(req.getParameter("class_name")!=null&&req.getParameter("class_name")!="") {
			class_name = req.getParameter("class_name");
		}
		System.out.println(class_name);
		
		PageHelper.startPage(startPage, PageSize);
		List<Plan> l =  new ArrayList<Plan>();
		l =  planService.getAllPlan(class_name);
		PageInfo<Plan> pageData = new PageInfo<Plan>(l);
		model.addAttribute("class_name",class_name);
		model.addAttribute("train_plan",pageData);
		
		return "forward:/train/plan.jsp";
	}
	
	@RequestMapping("/Allcourse")
	public String getCourseByPlanId(@RequestParam(required = false, defaultValue = "1") Integer startPage,
            @RequestParam(required = false, defaultValue = "5") Integer PageSize,Model model,@Param("id")Integer id,@Param("course_name")String course_name,HttpServletRequest req) {
		
		if(req.getParameter("course_name")!=null&&req.getParameter("course_name")!="") {
			course_name = req.getParameter("course_name");
		}
		System.out.println(course_name);
		
		Plan pl = planService.getOnePlanById(id);
		PageHelper.startPage(startPage, PageSize);
		List<Course> l = courseService.getCourseById(id,course_name);
		PageInfo<Course> pageData = new PageInfo<Course>(l);
		model.addAttribute("course",pageData);
		model.addAttribute("course_name",course_name);
		model.addAttribute("plan",pl);
		
		return "forward:/train/plan2course.jsp";
	}
	
	@RequestMapping("/Allstaff")
	public String getStaffByPlanid(@RequestParam(required = false, defaultValue = "1") Integer startPage,
            @RequestParam(required = false, defaultValue = "5") Integer PageSize,Model model,@Param("id")Integer id,@Param("staff_name")String staff_name,@Param("sex")String sex,HttpServletRequest req) {
		
		if(req.getParameter("staff_name")!=null&&req.getParameter("staff_name")!="") {
			staff_name = req.getParameter("staff_name");
		}
		if(req.getParameter("sex")!=null&&req.getParameter("sex")!="") {
			sex = req.getParameter("sex");
		}
		System.out.println("++++++++++++"+staff_name);
		System.out.println("++++++++++++"+sex);
		Plan pl = planService.getOnePlanById(id);
		PageHelper.startPage(startPage, PageSize);
		List<Staff> l = staffService.getStaffById(id,staff_name,sex);
		PageInfo<Staff> pageData = new PageInfo<Staff>(l);
		model.addAttribute("staff",pageData);
		model.addAttribute("plan",pl);
		model.addAttribute("staff_name",staff_name);
		model.addAttribute("sex",sex);
		return "forward:/train/plan2staff.jsp"; 
	}
	
	@RequestMapping("/updplanUI")
	public String updplanUI(HttpServletRequest req,Model model) {
		
		Integer planid = Integer.parseInt(req.getParameter("id"));
		Plan plan = planService.getOnePlanById(planid);
		model.addAttribute("plan",plan);
		System.out.println(plan);
		return "forward:/train/plan-upd.jsp";
	}
	
	@ResponseBody
	@RequestMapping("/updPlan")
	public String updPlan(Plan plan) {
		
		planService.updPlan(plan);
		return " ";
	}
	
	@RequestMapping("/addplanUI")
	public String addplanUI() {
		
		return "forward:/train/plan-add.jsp";
	}
	
	@ResponseBody
	@RequestMapping("/savePlan")
	public String addPlan(Plan plan) {
		
		planService.savePlan(plan);
		return " ";
	}
	
	@RequestMapping("/delPlan")
	public String delPlan(Integer[] id) {
		
		for (Integer pid : id) {
			
			System.out.println(pid);
		}
		
		planService.delPlan(id);
		
		return "";
	}
	
	@RequestMapping("/addC2PUI")
	public String addC2PUI(Model model,Integer pid){
		List<Course> l = courseService.getAllCourse();
		model.addAttribute("courseList",l);
		model.addAttribute("pid",pid);
		return "forward:/train/addC2P.jsp";	
	}
	
	@RequestMapping("/addC2P")
	public String addC2P(@Param("plan_id")Integer plan_id,@Param("course_id")Integer course_id) {
		planService.addCourse2Plan(plan_id, course_id);
		return " ";
	}
	
	@RequestMapping("/addS2PUI")
	public String addS2PUI(Model model,Integer pid){
		List<Staff> l = staffService.getAllStaff();
		model.addAttribute("staffList",l);
		model.addAttribute("pid",pid);
		return "forward:/train/addS2P.jsp";	
	}
	
	@RequestMapping("/addS2P")
	public String addS2P(@Param("plan_id")Integer plan_id,@Param("staff_id")Integer staff_id) {
		planService.addStaff2Plan(plan_id, staff_id);
		return " ";
	}
	
	@RequestMapping("/delC2P")
	public String delC2P(Integer pid,Integer[] course_id) {
		for (Integer cid : course_id) {
			
			System.out.println("============"+cid);
		}
		System.out.println("++++++++++++"+pid);
		System.out.println("++++++++++++"+course_id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pid", pid);
		map.put("course_id", course_id);
		System.out.println("--------------map"+map);
		planService.delCourse2Plan(map);
		
		return "";
	}
	
	@RequestMapping("/delS2P")
	public String delS2P(Integer pid,Integer[] staff_id) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("pid", pid);
		map.put("staff_id", staff_id);
		planService.delStaff2Plan(map);
		
		return "";
	}
}
